package com.xc.round2Assessment.petStore.pet.factory;

import com.xc.round2Assessment.petStore.pet.strategy.AnimalStrategy;
import com.xc.round2Assessment.petStore.pet.strategyContext.BuyContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Xc
 * @version 1.0
 * 生成上下文封装角色的工厂
 * 使用享元模式/ 单例模式保证对象的单例 //todo 享元和单例这俩有啥区别啊
 *
 */
public class ContextFactory {

    private static final Map<AnimalStrategy, BuyContext> maps = new ConcurrentHashMap<>();

    public static BuyContext getContext(AnimalStrategy animalStrategy) {
        // 如果 maps 中还未有当前要取用的对象， 那么就新建一个对象，并存放到 map 中
        if (maps.get(animalStrategy) == null) {
            BuyContext buyContext = new BuyContext(animalStrategy);
            maps.put(animalStrategy, buyContext);
        }

        return maps.get(animalStrategy);
    }


}
